<template>
  <div class="main-box">
    <div :class="['container', 'container-register', { 'is-txl': isLogin }]">
      <form>
        <h2 class="title">Create Account</h2>
        <span class="text">or use email for registration</span>
        <input class="form__input" type="text" placeholder="Name" v-model="registerForm.name" />
        <input
          class="form__input"
          type="password"
          placeholder="Password"
          v-model="registerForm.password"
          @blur="checkRepeatPassword"
        />
        <input
          class="form__input"
          type="password"
          placeholder="Repeat Password"
          v-model="registerForm.repeatPassword"
          @blur="checkRepeatPassword"
        />
        <div v-if="RepeatPasswordErrorMessage" class="error-message">
          {{ RepeatPasswordErrorMessage }}
        </div>
        <div class="primary-btn" @click="register">立即注册</div>
      </form>
    </div>
    <div :class="['container', 'container-login', { 'is-txl is-z200': isLogin }]">
      <form>
        <h2 class="title">Sign in to Website</h2>
        <span class="text">or use email for registration</span>
        <input
          class="form__input"
          type="text"
          placeholder="Username"
          v-model="loginForm.username"
        />
        <input
          class="form__input"
          type="password"
          placeholder="Password"
          v-model="loginForm.password"
        />
        <div class="primary-btn" @click="login">立即登录</div>
      </form>
    </div>
    <div :class="['switch', { login: isLogin }]">
      <div class="switch__circle"></div>
      <div class="switch__circle switch__circle_top"></div>
      <div class="switch__container">
        <h2>{{ isLogin ? 'Hello Friend !' : 'Welcome Back !' }}</h2>
        <p>
          {{
            isLogin
              ? 'Enter your personal details and start journey with us'
              : 'To keep connected with us please login with your personal info'
          }}
        </p>
        <div class="primary-btn" @click="isLogin = !isLogin">
          {{ isLogin ? '立即注册' : '立即登录' }}
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { userApi } from '@/api/api'
import type { RegisterData } from '@/types/request/api'
import type { LoginResponse } from '@/types/response/apiResponse'
import { useRouter } from 'vue-router'
import { userStore } from '@/stores/user'
import { message } from 'ant-design-vue'


const isLogin = ref(true)

const loginForm = ref({
  username: '',
  password: '',
})

const router = useRouter() // 在 setup() 中使用 useRouter
const store = userStore()

function login() {
  const loginData: RegisterData = {
    userAccount: loginForm.value.username,
    userPassword: loginForm.value.password,
  }
  userApi.login(loginData).then((res: LoginResponse) => {
    store.setUsername(res.name)
    // 跳转到首页
    router.push({ name: 'home' })
  })
}

const RepeatPasswordErrorMessage = ref()
const registerForm = ref({
  name: '',
  password: '',
  repeatPassword: '',
})

function checkRepeatPassword() {
  // 如果 registerFrom
  if (registerForm.value.password === '' || registerForm.value.repeatPassword === '') {
    return
  }
  if (registerForm.value.password !== registerForm.value.repeatPassword) {
    RepeatPasswordErrorMessage.value = '两次密码不一致'
  } else {
    RepeatPasswordErrorMessage.value = ''
  }
}

function register() {
  // 注册逻辑
  if (registerForm.value.password !== registerForm.value.repeatPassword) {
    RepeatPasswordErrorMessage.value = '两次密码不一致'
    return
  }
  const registerData: RegisterData = {
    userAccount: registerForm.value.name,
    userPassword: registerForm.value.password,
  }
  userApi.register(registerData).then(() => {
    message.info('注册成功')
  })
}
</script>

<style scoped>
.main-box {
  position: relative;
  width: 100vw;
  min-width: 1000px;
  min-height: 600px;
  height: 100vh;
  padding: 25px;
  box-sizing: border-box;
  background-color: #ecf0f3;
  box-shadow:
    10px 10px 10px #d1d9e6,
    -10px -10px 10px #f9f9f9;
  border-radius: 12px;
  overflow: hidden;
}

.container {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 0;
  width: 70%;
  height: 100%;
  padding: 25px;
  background-color: #ecf0f3;
  transition: all 1.25s;
}

.container form {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  width: 100%;
  height: 100%;
  color: #a0a5a8;
}

.container .title {
  font-size: 34px;
  font-weight: 700;
  line-height: 3;
  color: #181818;
}

.container .text {
  margin-top: 30px;
  margin-bottom: 12px;
}

/* input */
.container .form__input {
  width: 350px;
  height: 40px;
  margin: 4px 0;
  padding-left: 25px;
  font-size: 13px;
  letter-spacing: 0.15px;
  border: none;
  outline: none;
  background-color: #ecf0f3;
  transition: 0.25s ease;
  border-radius: 8px;
  box-shadow:
    inset 2px 2px 4px #d1d9e6,
    inset -2px -2px 4px #f9f9f9;
}

.container .form__input::placeholder {
  color: #a0a5a8;
}

.container-register {
  z-index: 100;
  left: 30%;
}

.container-login {
  left: 30%;
  z-index: 0;
}

.is-txl {
  left: 0;
  transition: 1.25s;
  transform-origin: right;
}

.is-z200 {
  z-index: 200;
  transition: 1.25s;
}

.switch {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 30%;
  padding: 50px;
  z-index: 200;
  transition: 1.25s;
  background-color: #ecf0f3;
  overflow: hidden;
  box-shadow:
    4px 4px 10px #d1d9e6,
    -4px -4px 10px #f9f9f9;
  color: #a0a5a8;
}

.switch__circle {
  position: absolute;
  width: 50vh;
  height: 50vh;
  border-radius: 50%;
  background-color: #ecf0f3;
  box-shadow:
    inset 8px 8px 12px #d1d9e6,
    inset -8px -8px 12px #f9f9f9;
  bottom: -250px;
  left: 25vh;
  transition: 1.25s;
}

.switch__circle_top {
  top: -25vh;
  left: 25vh;
  width: 50vh;
  height: 50vh;
}

.switch__container {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  position: absolute;
  width: 400px;
  padding: 50px 55px;
  transition: 1.25s;
}

.switch__container h2 {
  font-size: 34px;
  font-weight: 700;
  line-height: 3;
  color: #181818;
}

.switch__container p {
  font-size: 14px;
  letter-spacing: 0.25px;
  text-align: center;
  line-height: 1.6;
}

.login {
  left: 70%;
}

.login .switch__circle {
  left: 0;
}

.primary-btn {
  width: 180px;
  height: 50px;
  border-radius: 25px;
  margin-top: 50px;
  text-align: center;
  line-height: 50px;
  font-size: 14px;
  letter-spacing: 2px;
  background-color: #4b70e2;
  color: #f9f9f9;
  cursor: pointer;
  box-shadow:
    8px 8px 16px #d1d9e6,
    -8px -8px 16px #f9f9f9;
}

.primary-btn:hover {
  box-shadow:
    4px 4px 6px 0 rgb(255 255 255 / 50%),
    -4px -4px 6px 0 rgb(116 125 136 / 50%),
    inset -4px -4px 6px 0 rgb(255 255 255 / 20%),
    inset 4px 4px 6px 0 rgb(0 0 0 / 40%);
}

.input-with-error {
  display: flex;
  align-items: center;
}

.error-message {
  color: red;
  font-size: 12px;
  margin-left: 10px;
  white-space: nowrap; /* 防止错误信息换行 */
}
</style>
